<template>
  <div class="register-container">
    <el-card title="用户注册">
      <el-form ref="registerForm" :model="user" :rules="rules" label-width="100px">
        <!-- 用户名 -->
        <el-form-item label="用户名" prop="username">
          <el-input v-model="user.username" placeholder="请输入1-50位字母、数字或下划线"></el-input>
        </el-form-item>
        
        <!-- 密码 -->
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="user.password" placeholder="请输入密码"></el-input>
        </el-form-item>
        
        <!-- 真实姓名 -->
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="user.realName" placeholder="请输入真实姓名"></el-input>
        </el-form-item>
        
        <!-- 性别 -->
        <el-form-item label="性别" prop="gender">
          <el-radio-group v-model="user.gender">
            <el-radio label="M">男</el-radio>
            <el-radio label="F">女</el-radio>
          </el-radio-group>
        </el-form-item>
        
        <!-- 手机号 -->
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="user.phone" placeholder="请输入手机号（选填）"></el-input>
        </el-form-item>
        
        <!-- 邮箱 -->
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="user.email" placeholder="请输入邮箱（选填）"></el-input>
        </el-form-item>
        
        <!-- 头像上传 -->
        <el-form-item label="头像" prop="avatar">
          <el-upload
            action="/api/upload/avatar"   
            :on-success="handleAvatarSuccess"
            :show-file-list="false"
            accept="image/*">
            <el-button size="small" type="primary">点击上传头像</el-button>
          </el-upload>
          <div v-if="user.avatar" class="avatar-preview">
            <img :src="user.avatar" alt="头像预览" style="width: 100px; height: 100px; margin-top: 10px;">
          </div>
        </el-form-item>
        
        <!-- 按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm">注册</el-button>
          <el-button @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import UserApi from '@/api/Ost/UserApi'
import Validator from '../../utils/Ost/Validator'

export default {
  data() {
    return {
      user: { 
        username: '', 
        password: '', 
        realName: '',
        gender: '',
        phone: '', 
        email: '', 
        avatar: ''
      },
      rules: {
        // 用户名规则
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { max: 50, message: '用户名长度不能超过50位', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9_]+$/, message: '只能包含字母、数字或下划线', trigger: 'blur' }
        ],
        // 密码规则
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 20, message: '密码长度必须为6-20位', trigger: 'blur' },
          { pattern: /^(?=.*[0-9])(?=.*[a-zA-Z]).+$/, message: '密码需同时包含字母和数字', trigger: 'blur' }
        ],
        // 真实姓名规则
        realName: [
          { required: true, message: '请输入真实姓名', trigger: 'blur' },
          { max: 50, message: '真实姓名长度不能超过50位', trigger: 'blur' }
        ],
        // 性别规则
        gender: [
          { validator: Validator.validateGender, trigger: 'change' }
        ],
        // 手机号规则
        phone: [
          { validator: Validator.validatePhone, trigger: 'blur' }
        ],
        // 邮箱规则
        email: [
          { validator: Validator.validateEmail, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm() {
      this.$refs.registerForm.validate(async (valid) => {
        console.log('校验结果:', valid); 
        if (valid) {
          console.log('开始发送请求，用户数据:', this.user);
          try {
            console.log('调用UserApi.register前'); 
            const res = await UserApi.register(this.user);
            console.log('请求成功，响应:', res);
            if (res.success) {
              this.$message.success(res.message);
            } else {
              this.$message.error(res.message);
            }
          } catch (err) {
            console.log('请求失败，错误详情:', err);
            this.$message.error('网络异常');
          }
        }
      });
    },
    resetForm() {
      this.$refs.registerForm.resetFields();
      this.user.avatar = ''; // 重置头像
    },
    handleAvatarSuccess(response) {
      // 假设上传成功后后端返回头像路径
      this.user.avatar = response.data;
    }
  }
}
</script>

<style>
.avatar-preview {
  margin-top: 10px;
}
</style>